package popcorn.ticket.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import popcorn.ticket.entity.Grades;
import popcorn.ticket.entity.Members;
import popcorn.ticket.service.GradesService;
import popcorn.ticket.service.MembersService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

@Controller
public class MemberController {
	@Autowired
	MembersService membersService;
	@Autowired
	GradesService gradesService;

	// 分页
	@GetMapping(value = "memberspage", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Members> listBlogByPage1(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "keywords", defaultValue = "") String keywords) {
		// 判断传入的keywords的值是不是数字
		for (int i = 0; i < keywords.length(); i++) {
			if (Character.isDigit(keywords.charAt(i))) {
				return membersService.selectMembersByGradeId(page, limit, keywords);
			}
		}
		// 直接返回分页的数据
		return membersService.selectMembersByLike(page, limit, keywords);
	}

	// 修改
	@PostMapping(value = "updMembers", consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	// 参数位置获取前台传递的json值
	public AjaxResponse updUsers(@RequestBody Members members) {
		String msg = "修改失败";
		int integral = members.getIntegral();
		// 获取所有的积分
		List<Grades> list = gradesService.getGrades();
		for (int i = 0; i < list.size(); i++) {

			if (i < list.size() - 1) {
				if (list.get(i).getIntegralrange() <= integral && integral < list.get(i + 1).getIntegralrange()) {

					members.setGradeid(list.get(i).getGradeid());
				}
			}
		}

		if (members != null) {
			int flag = membersService.updateByPrimaryKeySelective(members);
			if (flag > 0) {
				msg = "修改成功";
			}
		}
		AjaxResponse ajaxResponse = new AjaxResponse(msg, 200);
		return ajaxResponse;
	}

	@RequestMapping("getMember/{userid}")
	@ResponseBody
	/**
	 * 获取用户的会员信息
	 * 
	 * @param userid
	 * @return
	 */
	public Members getMember(@PathVariable("userid") int userid) {

		return membersService.getMember(userid);
	}

	/**
	 * 获得会员等级积分
	 * 
	 * @param userid
	 * @return
	 */

	@RequestMapping(value = "offers", produces = "application/json;charset=utf-8")
	public ModelAndView getMemberIntegral(HttpSession session) {
		int userid = 0;
		// 判断用户是否登录,有登录则显示用户积分
		if (session.getAttribute("userid") != null) {
			// 获得用户id
			userid = (int) session.getAttribute("userid");
			ModelAndView mv = new ModelAndView("forward:offers.jsp");
			int a = membersService.getMember(userid).getIntegral();
			mv.addObject("integral", a);
			return mv;
		}

		ModelAndView mv = new ModelAndView("forward:offers.jsp");
		mv.addObject("integral", userid);
		return mv;
	}

}
